package org.itsolutions.javaBackup.gui.web;

import java.util.concurrent.atomic.AtomicBoolean;

import org.itsolutions.javaBackup.exceptions.AException;
import org.itsolutions.javaBackup.logger.ELogLevel;
import org.itsolutions.javaBackup.logger.ILogger;
import org.itsolutions.javaBackup.logger.LoggerHelper;
import org.itsolutions.javaBackup.spi.EModule;
import org.itsolutions.javaBackup.spi.IModuleStartStopTask;
import org.itsolutions.javaBackup.spi.gui.IGUIWebController;

import com.google.inject.Inject;
import com.google.inject.Stage;

/**
 * 
 * @author Tomas Fecko
 */
public final class GUIWebModuleStartStopTask implements IModuleStartStopTask {

	private static final ILogger 		LOGGER = LoggerHelper.getLogger(GUIWebModuleStartStopTask.class);	
	private static final AtomicBoolean	IS_STARTED = new AtomicBoolean(false);
	private final Stage					mStage;
	private final IGUIWebController		mGUIWebController;

	/**
	 * 
	 * @param pStage stage
	 * @param pGUIWebController web controller
	 */
	@Inject GUIWebModuleStartStopTask(Stage pStage, IGUIWebController pGUIWebController) {
		mStage = pStage;
		mGUIWebController = pGUIWebController;
	}

	@Override
	public void start() throws AException {
		LOGGER.log(ELogLevel.DEBUG, "Starting GUI web module.");
		if (mStage == Stage.DEVELOPMENT) {
			mGUIWebController.startWebServerInDevelopmentMode();
		} else if (mStage == Stage.PRODUCTION) {
			mGUIWebController.startWebServer();
		} else {
			LOGGER.log(ELogLevel.WARN, "Stage:", mStage, " unknown. Not starting GUI WEB module.");
			return;
		}
		IS_STARTED.set(true);		
	}

	@Override
	public void stop() throws AException {
		LOGGER.log(ELogLevel.DEBUG, "Stopping GUI web module.");
		mGUIWebController.stopWebServer();
		IS_STARTED.set(false);		
	}

	@Override
	public boolean isStarted() throws AException {
		return IS_STARTED.get();
	}

	@Override
	public EModule getModule() {
		return EModule.GUI_WEB;
	}
}




